<?php

namespace app\index\controller\workbench;

use app\index\model\UM as UM;
use app\index\service\ConsultingService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Consulting 控制器
 * Class Consulting
 * @package app\index\controller\workbench;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\index\validate\workbench\Consulting
 */
class Consulting
{
    // 请求参数
    private Request $request;

    // service
    private ConsultingService $service;

    public function __construct(Request $request, ConsultingService $consultingService)
    {
        $this->request = $request;
        $this->service = $consultingService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /workbench/consulting/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $consultingServerId = $request->param('consultingServerId');
        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        if (!$user) {
            return ApiResponse::success([]);
        }

        return ApiResponse::success($this->service->indexWorkbench($user, $consultingServerId, $page, $page_size));
    }

    /**
     * 确认已抢单咨询
     * @param UM $user
     * @return ApiResponse
     * @url /workbench/consulting/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');
        if (!is_id($id)) {
            throw new ValidateException('请选择一个咨询');
        }

        $this->service->confirm($user, $id);

        return ApiResponse::success(null);
    }
}
